package sf.dsl;

import sf.database.dialect.DBDialect;
import sf.database.dialect.DefaultDBDialect;
import sf.database.jdbc.sql.SQLContext;
import sf.database.jdbc.sql.SQLParameter;
import sf.database.meta.ColumnMapping;
import sf.dsl.example.DSLMethod;
import sf.dsl.example.Example;
import sf.dsl.example.ExampleSQL;
import sf.dsl.example.RawValue;
import sf.dsl.example.SQLFlag;
import sf.dsl.example.SelectOpt;
import sf.dsl.example.SimpleField;
import sf.dsl.example.SimpleTable;
import sf.dsl.example.ValueType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class InsertDSL {

    protected final SimpleTable entity;

    protected final List<SimpleField> columns = new ArrayList<>();
    protected final List<Object> values = new ArrayList<>();

    protected Example subQuery;

    protected final List<InsertBatch> batches = new ArrayList<>();

    protected DBDialect dialect = DefaultDBDialect.instance;

    protected SQLFlag sqlFlag = new SQLFlag();

    protected transient boolean batchToBulk;

    public InsertDSL(SimpleTable entity) {
        this.entity = entity;
    }

    public InsertDSL addFlag(SQLFlag.Position position, String flag) {
        this.sqlFlag.addFlag(position, flag);
        return this;
    }

    public InsertDSL columns(SimpleField... columns) {
        this.columns.addAll(Arrays.asList(columns));
        return this;
    }

    public InsertDSL select(Example sq) {
        subQuery = sq;
        return this;
    }

    public <T> InsertDSL set(SimpleField simpleField, T value) {
        columns.add(simpleField);
        values.add(value);
        return this;
    }

    public InsertDSL setNull(SimpleField simpleField) {
        columns.add(simpleField);
        values.add(null);
        return this;
    }

    public InsertDSL values(Object... v) {
        Collections.addAll(values, v);
        return this;
    }

    public InsertDSL addBatch() {
        batches.add(new InsertBatch(columns, values, subQuery));
        columns.clear();
        values.clear();
        subQuery = null;
        return this;
    }

    /**
     * Set whether batches should be optimized into a single bulk operation.
     * Will revert to batches, if bulk is not supported
     */
    public void setBatchToBulk(boolean b) {
        this.batchToBulk = b;
    }

    public InsertDSL setDialect(DBDialect dialect) {
        this.dialect = dialect;
        return this;
    }

    public SimpleTable getEntity() {
        return entity;
    }

    public List<SQLContext> getSQLContexts() {
        if (batches.isEmpty()) {
            SQLContext sqlContext = getSQLContextOne(sqlFlag, entity, columns, Collections.singletonList(values), subQuery, dialect);
            return Collections.singletonList(sqlContext);
        } else if (batchToBulk) {
            List<List<Object>> valueList = new ArrayList<>();
            InsertBatch one = null;
            for (InsertBatch batch : batches) {
                valueList.add(batch.getValues());
                one = batch;
            }
            SQLContext sqlContext = getSQLContextOne(sqlFlag, entity, one.getColumns(), valueList, one.getSubQuery(), dialect);
            return Collections.singletonList(sqlContext);
        } else {
            List<SQLContext> builder = new ArrayList<>();
            for (InsertBatch batch : batches) {
                SQLContext sqlContext = getSQLContextOne(sqlFlag, entity, batch.getColumns(), Collections.singletonList(batch.getValues()), batch.getSubQuery(), dialect);
                builder.add(sqlContext);
            }
            return builder;
        }
    }

    public int getCount() {
        if (!batches.isEmpty()) {
            return batches.size();
        } else {
            return 1;
        }
    }

    public boolean isBatchToBulk() {
        return batchToBulk;
    }

    public static SQLContext getSQLContextOne(SQLFlag sqlFlag, SimpleTable entity, List<SimpleField> columns, List<List<Object>> valueList, Example subQuery, DBDialect dialect) {
        StringBuilder sql = new StringBuilder();
        List<SQLParameter> list = new ArrayList<>();
        SQLContext sqlContext = new SQLContext();
        sqlFlag.toSql(SQLFlag.Position.START, sql, list);
        sql.append("insert into ");
        sqlFlag.toSql(SQLFlag.Position.AFTER_SELECT, sql, list);
        entity.toNoAliasSql(sql, list, dialect);
        sqlFlag.toSql(SQLFlag.Position.AFTER_PROJECTION, sql, list);
        if (!columns.isEmpty()) {
            sql.append('(');
            AtomicBoolean atomicBoolean = new AtomicBoolean(false);
            boolean flag = false;
            for (SimpleField field : columns) {
                if (flag) {
                    sql.append(',');
                }
                flag = true;
                field.toConditionSql(sql, list, atomicBoolean, false, dialect);
            }
            sql.append(')');
        }
        if (subQuery != null) {
            SQLContext subSqlContext = ExampleSQL.getSelectSQLContext(subQuery, null, SelectOpt.all, true);
            String subSql = subSqlContext.getSql();
            list.addAll(subSqlContext.getParas());
            sql.append(' ').append(subSql);
        } else {
            if (!valueList.isEmpty()) {
                sql.append(" values");
                boolean f1 = false;
                for (List<Object> values : valueList) {
                    if (f1) {
                        sql.append(',');
                    }
                    f1 = true;
                    sql.append('(');
                    boolean f2 = false;
                    for (int i = 0; i < values.size(); i++) {
                        Object value = values.get(i);
                        SimpleField simpleField = columns.get(i);
                        if (f2) {
                            sql.append(',');
                        }
                        f2 = true;
                        setValue(sql, list, new ColumnMapping[]{simpleField.getColumnMapping()}, value);
                    }
                    sql.append(')');
                }
            }
        }
        sqlFlag.toSql(SQLFlag.Position.END, sql, list);
        sqlContext.setSql(sql.toString());
        sqlContext.setParas(list);
        return sqlContext;
    }

    protected static void setValue(StringBuilder sql, List<SQLParameter> list, ColumnMapping[] cmList, Object value) {
        if (value instanceof RawValue) {//直接保存原始值
            sql.append(((RawValue) value).getContext());
        } else {
            ValueType valueType = ValueType.singleValue;
            int columnSize = cmList.length;
            if (columnSize > 1) {
                if (value instanceof Collection) {
                    valueType = ValueType.listValue;
                } else if (value.getClass().isArray()) {
                    valueType = ValueType.arrayValue;
                }
            }
            DSLMethod.addPlaceholderByValue(sql, columnSize, columnSize, value, valueType);
            DSLMethod.setSqlParameters(list, columnSize, cmList, columnSize, value, valueType);
        }
    }
}
